package com.cn.xc.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.ResourceUtils;

/**
 * FUN: file to other type
 *
 * @author sushy
 * @date 2016-3-24 
 */
public class FromFileUtil {
	public static Log logger = LogFactory.getLog(FromFileUtil.class);
	
	/**
	 * //# file convert to string ,default encode utf-8
	 * @param srcPath
	 * @return
	 */
	public static String readFileAndReturnRsString(String srcPath){
		if(StringUtil.isEmpty(srcPath)){
			logger.error(" file path is null !");
			return null;
		}
		File file = null;
		String str = null;
		try {
			file = ResourceUtils.getFile(srcPath);
			str = FileUtils.readFileToString(file, "UTF-8");
		} catch (FileNotFoundException e) {
			logger.error("path ["+srcPath+"], not found file!");
			e.printStackTrace();
		} catch (IOException e) {
			logger.error(srcPath+", readFileAndReturnRsString failed !");
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * //# file convert to string , you can custom encoding
	 * @param srcPath
	 * @return
	 */
	public static String readFileAndReturnRsString(String srcPath, String encoding){
		if(StringUtil.isEmpty(srcPath)){
			logger.error(" file path is null !");
			return null;
		}
		File file = null;
		String str = null;
		try {
			file = ResourceUtils.getFile(srcPath);
			str = FileUtils.readFileToString(file, encoding);
		} catch (FileNotFoundException e) {
			logger.error("path ["+srcPath+"], not found file!");
			e.printStackTrace();
		} catch (IOException e) {
			logger.error(srcPath+", readFileAndReturnRsString failed !");
			e.printStackTrace();
		}
		return str;
	}
}
